package cn.hzc.fortess.system.controller;

import cn.hzc.fortess.core.common.HttpContext;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


public abstract class BaseController {

    protected final String REDIRECT = "redirect:";
    protected final String FORWARD = "forward:";

    private final static String PAGE_BASE_PATH = "/system/";
    protected final static String KEY_SESSION_CURRENT_PROJECT_ID="currentProjectId";

    protected abstract String moduleName();


    protected HttpServletRequest getHttpServletRequest() {
        return HttpContext.getRequest();
    }

    protected HttpServletResponse getHttpServletResponse() {
        return HttpContext.getResponse();
    }

    protected HttpSession getSession() {
        return getHttpServletRequest().getSession();
    }

    protected HttpSession getSession(Boolean flag) {
        return getHttpServletRequest().getSession(flag);
    }

    protected String getParam(String name) {
        return getHttpServletRequest().getParameter(name);
    }

    protected void setAttr(String name, Object value) {
        getHttpServletRequest().setAttribute(name, value);
    }


    protected String buildToView(String viewName) {
        return PAGE_BASE_PATH.concat(moduleName()).concat("/").concat(viewName);
    }


    protected void deleteAllCookie() {
        Cookie[] cookies = this.getHttpServletRequest().getCookies();
        if (cookies == null) {
            return;
        }
        for(Cookie cookie:cookies){
            Cookie temp = new Cookie(cookie.getName(), "");
            temp.setMaxAge(0);
            this.getHttpServletResponse().addCookie(temp);
        }
    }
}
